using TMPro;
using UnityEngine;
using UnityEngine.UI;
using TEngine;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;

namespace GameLogic
{
	partial class SolarSystemMapUI: UIGameWindow
	{
        string m_PlaceIconPath = "Assets/AssetRaw/UI/Map/MapIcon.prefab";
        List<MapIcon> m_Icons = new List<MapIcon>();
        MapPlaceInformationPanel m_MapPlaceInformationPanel;

        float m_ImageScaleMin = 0.001f;
        float m_ImageScaleMax = 10;
        float m_MapScale = 1;
        #region 
        protected override void OnCreate()
        {
            base.OnCreate();
            var root = gameObject.transform.Find("Root");
            m_MapPlaceInformationPanel = CreateWidgetByPath<MapPlaceInformationPanel>(root, "Assets/AssetRaw/UI/Map/MapPlaceInformationPanel.prefab"); 
        }
        protected override void OnUpdate()
        {
            base.OnUpdate();
            m_tfBG.gameObject.transform.localPosition = m_tfConent.transform.localPosition;
        }

        //* m_tfConent.GetComponent<RectTransform>().sizeDelta.y *
        protected override void RegisterEvent()
        {
            base.RegisterEvent();
            var eventTrigger = m_tfViewer.GetComponent<EventTriggerEx>();
            m_btnExBack.AddClickEvent(() =>
            {
                GameModule.UI.CloseUI<SolarSystemMapUI>();
                //GameEvent.Send(GameEventType.OnBackClicked);
            });
            eventTrigger.OnScrollCallback.AddListener(OnScroll);
            eventTrigger.OnDragCallback.AddListener(OnDrag);
        }

        void OnDrag(PointerEventData eventData)
        {
            m_tfConent.GetComponent<RectTransform>().localPosition += new Vector3(eventData.delta.x, eventData.delta.y, 0);
        }
        void OnScroll(PointerEventData eventData)
        {
            float axis = Input.GetAxis("Mouse ScrollWheel");
            var mousePos = GetMousePos();
            var imageSize = new Vector3(m_tfConent.GetComponent<RectTransform>().rect.width,
                m_tfConent.GetComponent<RectTransform>().rect.height, 0);
            var xScale = mousePos.x / imageSize.x;
            var yScale = mousePos.y / imageSize.y;
            var newImageScale = m_MapScale + axis * Time.deltaTime * m_MapScale * 100;
            newImageScale = Mathf.Clamp(newImageScale, m_ImageScaleMin, m_ImageScaleMax);
            var scaleDifference = (newImageScale - m_MapScale);
            m_tfConent.transform.localPosition -= (new Vector3(xScale, yScale, 0) * (scaleDifference * imageSize.x));
            m_MapScale = newImageScale;
            m_tfConent.GetComponent<RectTransform>().localScale = new Vector3(m_MapScale, m_MapScale, 1);

        }
        Vector2 GetMousePos()
        {
            Vector2 mousePos = Input.mousePosition;
            Vector2 vecMouse = Input.mousePosition;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(m_tfConent.GetComponent<RectTransform>(), Input.mousePosition, GameModule.UI.UICamera, out vecMouse);

            return vecMouse;
        }
        protected override void OnRefresh()
        {
            base.OnCreate();
            if(m_Icons.Count > 0)
            {
                foreach(var i in m_Icons)
                {
                    PoolManager.Instance.PushGameObject(i.gameObject);
                }
            }
            m_Icons.Clear();
            foreach (var i in SolarSystemModule.Instance.StarSpheres)
            {
                var widget = CreateWidgetByPath<MapIcon>(m_tfConent, m_PlaceIconPath);
                widget.Bind(i);
                widget.OnClickCallback.AddListener(OnPlaceIconClicked);
                m_Icons.Add(widget);
            }
        }
        void OnPlaceIconClicked(StarSphere starSphere)
        {
            m_MapPlaceInformationPanel.Open();
            m_MapPlaceInformationPanel.SwitchPlace(starSphere);
        }
        #endregion
    }
}